﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Web.UI;
using BusinessLogic.Command;

namespace CO9APPFORM
{
    public abstract class BasePage : System.Web.UI.Page
    {
        public APP_USERS CurrentUser
        {
            get
            {
                if (Session["CurrentUser"] == null)
                {
                    Response.Redirect("Default.aspx");

                }

                return (APP_USERS)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }

        public void NeedAllowRole(string role, string targetPage)
        {
            if (CurrentUser.role != role)
                Response.Redirect(targetPage);
        }

        public void NotAllowRole(string role, string targetPage)
        {
            if (CurrentUser.role == role)
                Response.Redirect(targetPage);
        }

        public void ShowControlWith(Control ctrl, string role)
        {
            if (CurrentUser.role != role)
                ctrl.Visible = false;
        }

        public void HideControlWith(Control ctrl, string role)
        {
            if (CurrentUser.role == role)
                ctrl.Visible = false;
        }        

        protected void Page_Init(object sender, EventArgs e)
        {
            //Bypass("admin", "admin");            
        }

        protected void Bypass(string uname, string pwd)
        {
            GetUserCommand cmd = new GetUserCommand(uname, pwd);
            cmd.Execute();
            Session["CurrentUser"] = cmd.Result;
        }
    }
}